#include "characterclass.h"
#include "skill.h"
#include "classlevel.h"
#include "characterfeature.h"


#include <QTimer>
#include <QMetaProperty>
#include <QStringList>


CharacterClass::CharacterClass(QObject *parent) :
    DTwentyItem(parent)
{
    qRegisterMetaType<CharacterFeatures*>();
    qRegisterMetaType<ClassLevels*>();
    qRegisterMetaType<Skills*>();

    QTimer::singleShot(0, this, SLOT(onInitChildren()));

}

CharacterClass::CharacterClass(const CharacterClass& other) :
    DTwentyItem(other)
{
}

int CharacterClass::type() const
{
    return qMetaTypeId<CharacterClass*>();
}


QString CharacterClass::abbreviation() const
{
    QStringList parts = name().split(" ");
    QString abbr;
    if (parts.count() == 0)
    {
        abbr = "[?]";
    }
    else if (parts.count() == 1)
    {
        abbr = parts.first().left(3);
    }
    else
    {
        abbr = parts.value(0).left(1) + parts.value(1).left(2);
    }

    return abbr;
}


void CharacterClass::onInitChildren()
{

    QString featureName = tr("Features");
    QString levelName = tr("Levels");
    QString skillsName = tr("Class Skills");

    initChild<CharacterFeatures>(featureName);

    initChild<ClassLevels>(levelName);


    Skills* classSkills = initChild<Skills>(skillsName);
    Skills* campaignSkills = rootItem()->findChild<Skills*>(QString(), Qt::FindDirectChildrenOnly);

    if (!campaignSkills)
    {
        return;
    }

    classSkills->setValueSource(campaignSkills);
}

